/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.lswms.controller;

import com.lswms.annotation.Login;
import com.lswms.annotation.LoginUser;
import com.lswms.common.redis.RedisUtils;
import com.lswms.common.utils.Result;
import com.lswms.common.validator.ValidatorUtils;
import com.lswms.dto.YnUserDTO;
import com.lswms.dto.vo.req.LoginReq;
import com.lswms.dto.vo.req.UpdateUserInfoReq;
import com.lswms.dto.vo.resp.LoginResp;
import com.lswms.dto.vo.resp.UpDateInfo;
import com.lswms.entity.YnUserEntity;
import com.lswms.redis.SysParamsRedis;
import com.lswms.service.YnUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;










import javax.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import springfox.documentation.annotations.ApiIgnore;

import com.alibaba.fastjson.JSON;

/**
 * 登录接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/api/user")
@Api(tags="登录接口")
@Slf4j
public class YnUserController {
	
    @Autowired
    private SysParamsRedis sysParamsRedis;
    
    @Autowired
    private YnUserService ynUserService;

    /**
     * 登录、注册
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("login")
    @ApiOperation("登录、注册")
    public Result<LoginResp> login(@RequestBody LoginReq dto,HttpServletRequest request){
        //表单校验
        ValidatorUtils.validateEntity(dto);
        log.info("登录、注册：参数：{}",JSON.toJSONString(dto));
        LoginResp resp = ynUserService.login(dto, request);
        log.info("登录、注册：返回：{}",JSON.toJSONString(resp));
        return new Result<LoginResp>().ok(resp);
    }
    /**
     * 获取用户信息
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("userInfo")
    @ApiOperation("获取用户信息")
    @Login
    public Result<YnUserDTO> userInfo(@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
        log.info("获取用户信息：参数：{}");
        YnUserDTO resp = ynUserService.queryUserInfo(ynuser);
        log.info("获取用户信息：返回：{}",JSON.toJSONString(resp));
        return new Result<YnUserDTO>().ok(resp);
    }

    /**
     * 修改用户信息
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("updateUserInfo")
    @ApiOperation("修改用户信息")
    @Login
    public Result<Object> updateUserInfo(@RequestBody UpdateUserInfoReq dto,@ApiIgnore @LoginUser YnUserEntity ynuser,HttpServletRequest request){
        log.info("修改用户信息：参数：{}");
        ynUserService.updateUserInfo(dto,request,ynuser);
        return new Result<Object>();
    }

    @SuppressWarnings({ "rawtypes", "unchecked"})
	@ApiOperation(value="上传图片", notes="上传图片")
    @PostMapping(value = "/uploadImg")
    public Result uploadImage(HttpServletRequest request) throws Exception {
        log.info("上传图片：");
        String url = ynUserService.uploadImage(request);
        log.info("上传图片，url:{}",url);
        return new Result().ok(url);
    }
    /**
     * 后台控制版本更新
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("saveAppVision")
    @ApiOperation("后台控制版本更新")
    public Result<Object> saveAppVision(@RequestBody UpDateInfo dto,HttpServletRequest request){
    	log.info("后台控制版本更新：参数：{}",JSON.toJSONString(dto));
    	
    	sysParamsRedis.set("version", dto.getVersion());
    	sysParamsRedis.set("mold", dto.getMold()+"");
    	sysParamsRedis.set("info", dto.getUpgradeInfo());
    	sysParamsRedis.set("url", dto.getDownloadurl());
    	
        return new Result<Object>();
    }

    /**
     * appVision
     * @param dto
     * @param request
     * @return
     */
    @PostMapping("appVision")
    @ApiOperation("版本更新")
    public Result<UpDateInfo> appVision(HttpServletRequest request){
    	UpDateInfo resp = new UpDateInfo();
    	resp.setVersion(sysParamsRedis.get("version"));
    	resp.setMold(Integer.parseInt(sysParamsRedis.get("mold")));
    	resp.setUpgradeInfo(sysParamsRedis.get("info"));
    	resp.setDownloadurl(sysParamsRedis.get("url"));
    	

        log.info("appVision信息：返回：{}",JSON.toJSONString(resp));
        return new Result<UpDateInfo>().ok(resp);
    }


}